﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;
using System.ServiceModel;
using Pub.Lib;
using System.ServiceModel.Channels;
using WCFGzip;
using WCFContracts;
using System.Xml;
using Pub.Tools;
using Pub;
using System.Net;
using System.IO;
using System.ServiceModel.Description;

namespace WCFService
{
    public partial class WCFService : ServiceBase
    {
        public WCFService()
        {
            InitializeComponent();
        }

        ServiceHost host = new ServiceHost(typeof(WCFAccess));
        string ServerPort = "8888";
        string ListenPort = "8889";

        protected override void OnStart(string[] args)
        {

            WriteLog(1, "", "WCF服务启动" );

            string FileDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase.ToString();
            string SysSetXml = System.IO.Path.Combine(FileDir, @"xml\SysSet.xml");
            string Binding = DESEncrypt.Decrypt(XmlTools.Read(SysSetXml, "SysSet/WCF/Binding"));

            PubVar.GetSYSDAT();
            try
            {
                PubVar.Domain = Helper.DBHelper.GetInf("domain", PubVar.ConnMain, DataAccessType.CS).Trim();
            }
            catch (Exception ex)
            {
                SysTools.WriteTxt(PubVar.LogFile, "读取domain出错:" + ex.Message.ToString());
            }

            ServerPort = Pub.Tools.XmlTools.Read(SysSetXml, "SysSet/ServerPort");


            ListenPort = (SysTools.ToInt(ServerPort) + 1).ToString();
  
            string _UpdateSQL = XmlTools.Read(SysSetXml, "SysSet/UpdateSQL");

            PubVar.SysUpdateSQL = SysTools.ToBoolean(_UpdateSQL);


            string strMess = string.Empty;

            if (Binding.ToLower() == "ZipCustom".ToLower())
            {
                // ZipCustom
                strMess = Pub.Lib.WCFServer.ZipCustom(ref host, ServerPort, ListenPort);
            }
            else
            {
                // Basic
                strMess = Pub.Lib.WCFServer.Basic(ref host, ServerPort, ListenPort);
            }

            WriteLog(1, "", strMess);
            SysTools.WriteEventLog("WCF Service", strMess);
        }

        protected override void OnStop()
        {
            WriteLog(1, "", "WCF服务结束");
        }


        private void WriteLog(int iType, string strpc, string strLog)
        {

             //   string FileDir = System.Environment.CurrentDirectory.ToString();

       //         string FileDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase.ToString();

               string FileName =  Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase.ToString(), @"xml\log.xml");

                XmlDocument xmlDoc = new XmlDocument();            
   
                if (!System.IO.File.Exists(@FileName))
                { return; }
                xmlDoc.Load(@FileName);

                DataTable dtt = XmlDB.XmlToDataTable(xmlDoc.OuterXml);
               
                DataRow dr = dtt.NewRow();
                dr["dDate"] = System.DateTime.Now;
                dr["iType"] = iType;
                dr["cIP"] = strpc;
                dr["cText"] = strLog;

                dtt.Rows.Add(dr);


                xmlDoc.LoadXml(XmlDB.DataToXml(dtt));

                xmlDoc.Save(@FileName);
 
        }

    }
}
